package com.ptms.controller;

import com.ptms.dto.CoachRegisterDTO;
import com.ptms.dto.LoginDTO;
import com.ptms.dto.StudentRegisterDTO;
import com.ptms.service.AuthService;
import com.ptms.vo.ResultVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;

@RestController
@RequestMapping("/auth")  // 注意：前端请求是 /api/auth/xxx，需确认后端是否有统一前缀（如 application.yml 配置 server.servlet.context-path=/api）
@Validated  // 开启参数校验
public class AuthController {

    // 1. 声明 AuthService 成员变量
    private final AuthService authService;

    // 2. 手动编写构造函数（Spring 会通过构造函数注入 AuthService）
    public AuthController(AuthService authService) {
        this.authService = authService;  // 显式初始化，解决“未初始化”问题
    }

    // 登录接口（前端请求：POST /api/auth/login）
    @PostMapping("/login")
    public ResultVO<?> login(@Valid @RequestBody LoginDTO loginDTO) {
        return authService.login(loginDTO);
    }

    // 学员注册接口（前端请求：POST /api/auth/register/student）
    @PostMapping("/register/student")
    public ResultVO<?> registerStudent(@Valid @RequestBody StudentRegisterDTO studentDTO) {
        return authService.registerStudent(studentDTO);
    }

    // 教练注册接口（前端请求：POST /api/auth/register/coach）
    @PostMapping("/register/coach")
    public ResultVO<?> registerCoach(@Valid @RequestBody CoachRegisterDTO coachDTO) {
        return authService.registerCoach(coachDTO);
    }
}